package com.ruoyi.yx.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.entity.YxAndroidApplicationVo;
import com.ruoyi.common.core.domain.entity.YxAndroidDeviceVo;
import com.ruoyi.common.core.domain.entity.YxDeviceNoticeMessageVo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.*;
import com.ruoyi.system.domain.YxAdvertisement;
import com.ruoyi.system.domain.YxDeviceLocation;
import com.ruoyi.system.domain.YxUserAndDevice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api("微信小程序api")
@RequestMapping("/api/wx")
public class WXController extends BaseController {

    @Autowired
    private WeChatService weChatService;

    @Autowired
    private AppInfoService appInfoService;

    @Autowired
    private ControlService controlService;

    @Autowired
    private LocationService locationService;

    @Autowired
    private DeviceService deviceService;

    @Autowired
    private DeviceNoticeMessageService deviceNoticeMessageService;

    @Autowired
    private FeedBackService feedBackService;

    @Autowired
    private VipRechargeService vipRechargeService;


    /**
     * 修改用户
     */
    @PostMapping("/update/userinfo")
    public R<String> updateProfile(@RequestBody SysUser user)
    {
        return weChatService.updateNickNameAndAvater(user);
    }

    /**
     * code登录获取用户openid
     * @param json
     * @return
     * @throws Exception
     */
    @PostMapping("/codeLogin")
    @ApiOperation("微信code登录")
    public R<String> login(@RequestBody Map<String, Object> json)throws Exception{
        return weChatService.wxLogin((String) json.get("code"));
    }

    /**
     * 通过 code 绑定用户手机号
     * @param json
     * @return
     * @throws Exception
     */
    @PostMapping("/bindPhoneNumber")
    @ApiOperation("微信code登录")
    public R<String> bindPhoneNumber(@RequestBody Map<String, Object> json)throws Exception{
        return weChatService.bindPhoneNumber((String) json.get("code"));
    }

    /**
     * 通过deviceId 生成uuid 去绑定设备
     * @param deviceId
     * @return
     * @throws Exception
     */
    @PostMapping("/getUuui2BindUser")
    @ApiOperation("通过deviceId 生成uuid 去绑定设备")
    public R<String> wxGetUuui2BindUser(@RequestBody String deviceId)throws Exception{
        return deviceService.wxGetUuui2BindUser(deviceId);
    }

    /**
     * 获取用户信息
     *
     * @return 用户信息
     */
    @PostMapping("getInfo")
    @ApiOperation("获取用户信息")
    public AjaxResult getInfo() {
        SysUser user = SecurityUtils.getLoginUser().getUser();

        List vipList  = vipRechargeService.userVipStatusList();
        YxUserAndDevice defaultDevice  = deviceService.getDefaultDevice();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("vipList", vipList);
        ajax.put("defaultDevice", defaultDevice);

        return ajax;
    }

    /**
     * 扫描绑定设备
     * @param json
     * @return
     * @throws Exception
     */
    @PostMapping("/scanBindDevice")
    @ApiOperation("扫描绑定设备")
    public R<String> scanBindDevice(@RequestBody Map<String, Object> json)throws Exception{
        return weChatService.scanBindDevice((String) json.get("uuid"));
    }

    /**
     * 获取广告列表
     * @return
     * @throws Exception
     */
    @PostMapping("/advertisement/list")
    @ApiOperation("获取广告列表")
    public R<List<YxAdvertisement>> advertisementList()throws Exception{
        return weChatService.advertisementList();
    }

    /**
     * 获取设备当天使用时长
     * @return
     * @throws Exception
     */
    @PostMapping("/appUsage/list")
    @ApiOperation("获取设备当天使用时长")
    public R<Map> appUsageList()throws Exception{
        return appInfoService.appUsageList();
    }

    /**
     * 获取设备状态
     * @return
     * @throws Exception
     */
    @PostMapping("/device/info")
    @ApiOperation("获取设备当天使用时长")
    public R<Map> deviceInfo()throws Exception{
        return deviceService.wxDeviceInfo();
    }


    /**
     * 授权应用
     * @return
     * @throws Exception
     */
    @PostMapping("/app/authorizeAppSave")
    @ApiOperation("授权应用")
    public R<String> authorizeAppSave(@RequestBody List<YxAndroidApplicationVo> appList)throws Exception{
        return appInfoService.authorizeAppSave(appList);
    }

    /**
     * 获取设备全量应用列表
     * @return
     * @throws Exception
     */
    @PostMapping("/app/list")
    @ApiOperation("获取设备全量应用列表")
    public R<List<YxAndroidApplicationVo>> appList()throws Exception{
        return appInfoService.appList();
    }

    /**
     * 获取 授权 应用列表
     * @return
     * @throws Exception
     */
    @PostMapping("/app/authorizeList")
    @ApiOperation("获取 授权 应用列表")
    public R<List<YxAndroidApplicationVo>> appAuthorizeList()throws Exception{
        return appInfoService.appAuthorizeList();
    }

    /**
     * 绑定设备 cid
     * @return
     * @throws Exception
     */
    @PostMapping("/bindCid")
    @ApiOperation("获取 临时uuid 去绑定设备")
    public R<String> bindCid(@RequestBody Map<String, String> json)throws Exception{
        return weChatService.bindCid(json.get("cid"));
    }

    /**
     * 获取 默认设备坐标位置
     * @return
     * @throws Exception
     */
    @PostMapping("/location/get")
    @ApiOperation("获取 默认设备坐标位置")
    public R<YxDeviceLocation> getDeviceLocation()throws Exception{
        return locationService.getDeviceLocation();
    }

    /**
     * 查询设备通知消息列表
     */
    @GetMapping("/deviceNoticeMessage/list")
    @ApiOperation("查询设备通知消息列表")
    public TableDataInfo list() {

        List<YxDeviceNoticeMessageVo> list = deviceNoticeMessageService.deviceNoticeMessageList();
        return getDataTable(list);
    }

    /**
     * 获取绑定设备列表
     * @return
     * @throws Exception
     */
    @PostMapping("/device/list")
    @ApiOperation("获取绑定设备列表")
    public R<List<YxAndroidDeviceVo>> getDeviceList()throws Exception{
        return deviceService.getDeviceList();
    }

    /**
     * 修改学生姓名
     * @return
     * @throws Exception
     */
    @PostMapping("/device/editStuName")
    @ApiOperation("修改学生姓名")
    public R<String> editStuName(@RequestBody YxAndroidDeviceVo yxAndroidDeviceVo)throws Exception{
        return deviceService.editStuName(yxAndroidDeviceVo.getId(),yxAndroidDeviceVo.getStuName());
    }

    /**
     * 切换设备管控模式
     * @return
     * @throws Exception
     */
    @PostMapping("/device/switchDeviceControlMode")
    @ApiOperation("切换设备管控模式")
    public R<String> switchDeviceControlMode(@RequestBody Integer mode)throws Exception{
        return controlService.switchDeviceControlMode(mode);
    }

    /**
     * 切换默认设备
     * @return
     * @throws Exception
     */
    @PostMapping("/device/switchDefault")
    @ApiOperation("切换默认设备")
    public R<String> switchDefaultDevice(@RequestBody Long dId)throws Exception{
        return deviceService.switchDefaultDevice(dId);
    }

    /**
     * 解绑设备
     * @return
     * @throws Exception
     */
    @PostMapping("/device/unBindDevice")
    @ApiOperation("解绑设备")
    public R<String> unBindDevice(@RequestBody Long dId)throws Exception{
        return deviceService.unBindDevice(dId);
    }

    /**
     * 意见反馈
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadFeedBack")
    @ApiOperation("意见反馈")
    public R<String> uploadFeedBack(@RequestBody String content)throws Exception{
        return feedBackService.uploadFeedBack(content);
    }


}
